#pragma once
#include <QWidget>
#include <QGLWidget>
#include <QKeyEvent>
#include <QWheelEvent>
#include <QMouseEvent>
#include "Mesh.h"
#include "ImportData.h"
#include <vmmlib/matrix.hpp>
#include <vmmlib/vector.hpp>
#include "RidgeDetector.h"
#include <queue>
#include <Eigen/Eigen>
#include "GridElement.h"
#include "Edge.h"

using namespace Eigen;
using namespace std;

class GLRidgeWidget : public QGLWidget {
	 Q_OBJECT        
public:
	GLRidgeWidget(QWidget *parent);
	GLRidgeWidget();
	~GLRidgeWidget(void);

public slots:
	void updateAlpha(int a);
	void updateSlice(int s);
	void updatePointsize(QString s);
	void updateLinesize(QString s);
	void toggleGrid(bool checked);
	void toggleNet(bool checked);
	void toggleRidgePoints(bool checked);
	void toggleRidgeLines(bool checked);
	void toggleVolume(bool checked);
	void toggleBackdrop(bool checked);
	void toggleMostconvex(bool checked);
	void toggleDimension(bool checked);
	void loadData();
	void exportToObject();

signals:
	void setProgressFinish(int f);
	void updateProgress(int p);

private:
	std::vector<VectorXd> points,lstart,lend,line_color,point_color;
	std::vector<std::vector<VectorXd>> box;
	ImportData* flow_data;
	double alpha,pointsize,linesize;
	bool drawbackplate,show_2d,gridnet;
	bool showRidgeLines,showRidgePoints,whiteBackdrop,showVolume;
	int slice;
	Mesh m;
	string filepath;
	RidgeDetector* detector;

    // Camera movements
    QPointF prevMousePos;
    bool tracking1, tracking2;
    float cameraRotX;
    float cameraRotY;
    float cameraTrX;
    float cameraTrY;
    float cameraZoom;
    
	void generateMesh2d(int slice);
	void generateMesh3d();
	void drawBoundingBox();
	void drawSlice();
	void drawPoint(VectorXd& point,VectorXd c = Vector3d(0.0,1.0,0.0));
	void drawLine(VectorXd start,VectorXd end,VectorXd c = Vector3d(0.0,1.0,0.0));
	void drawRidgePoints();
	void drawRidgePoints3d();
	void drawRidgeLines();
	void drawSurface();
	void initializeGL();
	void resizeGL(int w,int h);
	void paintGL();
	
	void keyPressEvent(QKeyEvent* ev);
	void wheelEvent(QWheelEvent* ev);
	void mousePressEvent(QMouseEvent* ev);
	void mouseReleaseEvent(QMouseEvent* ev);
	void mouseMoveEvent(QMouseEvent* ev);

};
